﻿using BackData;
using Domain;
using Infratructure;
using MediatR;
using Server.API.Application.Command.Goods;

namespace Server.API.Application.Handler.Goods
{
    public class GoodsMoreDelHandler:IRequestHandler<GoodsMoreDelCommand,APIResult<int>>
    {
        private readonly IRepository<GoodsModel> goodsway;
        private readonly ILogger<GoodsMoreDelHandler> log;

        public GoodsMoreDelHandler(IRepository<GoodsModel> goodsway, ILogger<GoodsMoreDelHandler> log) 
        {
            this.goodsway = goodsway;
            this.log = log;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<int>> Handle(GoodsMoreDelCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res=new APIResult<int>();
            List< long>  ids=request.Ids.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(x=>long.Parse(x)).ToList();
            int count = 0;
            foreach (var Id in ids)
            {
                var value=await goodsway.GetValue(Id);
                value.IsDel = true;
                count+=await goodsway.Updateway(value);
            }
            if(count > 0)
            { 
                log.LogInformation("商品删除成功");
                res.Num = StateCode.OK;
                res.Message = "删除成功";
                res.Data = count;

            }
            else
            {
                log.LogError("商品删除失败");              
                res.Num = StateCode.Fail;
                res.Message = "删除失败";
            }
                return res;
        }
    }
}
